﻿package morn.core.managers
{
    import __AS3__.vec.*;
    import com.sh.game.consts.*;
    import com.sh.game.display.*;
    import com.sh.game.global.*;
    import com.sh.game.util.*;
    import flash.display.*;
    import flash.system.*;
    import morn.core.utils.*;

    public class AssetManager extends Object
    {
        private var _bmdMap:Object;
        private var _clipsMap:Object;
        private var _xmlClipMap:Object;
        private var _domain:ApplicationDomain;

        public function AssetManager()
        {
            this._bmdMap = {};
            this._clipsMap = {};
            this._xmlClipMap = {};
            this._domain = ApplicationDomain.currentDomain;
            return;
        }// end function

        public function hasClass(param1:String) : Boolean
        {
            return this._domain.hasDefinition(param1);
        }// end function

        public function getClass(param1:String) : Class
        {
            if (this.hasClass(param1))
            {
                return this._domain.getDefinition(param1) as Class;
            }
            return null;
        }// end function

        public function getAsset(param1:String)
        {
            var _loc_2:* = this.getClass(param1);
            if (_loc_2 != null)
            {
                return new _loc_2;
            }
            return null;
        }// end function

        public function getBitmapData(param1:String, param2:Boolean = true) : BitmapData
        {
            var _loc_4:Class = null;
            var _loc_3:* = this._bmdMap[param1];
            if (_loc_3 == null)
            {
                _loc_4 = this.getClass(param1);
                if (_loc_4 == null)
                {
                    return null;
                }
                _loc_3 = new _loc_4(1, 1);
                if (param2)
                {
                    this._bmdMap[param1] = _loc_3;
                }
            }
            return _loc_3;
        }// end function

        public function getClips(param1:String, param2:int, param3:int, param4:Boolean = true) : Vector.<BitmapData>
        {
            var _loc_6:BitmapData = null;
            var _loc_5:* = this._clipsMap[param1];
            if (this._clipsMap[param1] == null)
            {
                _loc_6 = this.getBitmapData(param1, false);
                if (_loc_6 == null)
                {
                    return null;
                }
                _loc_5 = BitmapUtils.createClips(_loc_6, param2, param3);
                _loc_6.dispose();
                if (param4)
                {
                    this._clipsMap[param1] = _loc_5;
                }
            }
            return _loc_5;
        }// end function

        public function getXMLClips(param1, param2:XML, param3:Boolean = true, param4:Boolean = true) : XMLClipData
        {
            var _loc_6:BitmapData = null;
            var _loc_7:Class = null;
            var _loc_5:* = this._xmlClipMap[param1];
            if (this._xmlClipMap[param1] == null)
            {
                if (param1 is String)
                {
                    if (!this._domain)
                    {
                        _loc_6 = this.getBitmapData(param1, param4);
                    }
                    else
                    {
                        if (!this._domain.hasDefinition(param1))
                        {
                            if (Config.Log > LogType.ERROR)
                            {
                                Logger.log("缺少资源" + param1, LogType.ERROR);
                            }
                            return null;
                        }
                        _loc_7 = this._domain.getDefinition(param1) as Class;
                        _loc_6 = new _loc_7 as BitmapData;
                    }
                }
                if (_loc_6 == null)
                {
                    return null;
                }
                _loc_5 = new XMLClipData(_loc_6, param2, param3);
                _loc_6.dispose();
                if (param4)
                {
                    this._xmlClipMap[param1] = _loc_5;
                }
            }
            return _loc_5;
        }// end function

        public function cacheBitmapData(param1:String, param2:BitmapData) : void
        {
            if (param2)
            {
                this._bmdMap[param1] = param2;
            }
            return;
        }// end function

        public function disposeBitmapData(param1:String) : void
        {
            var _loc_2:* = this._bmdMap[param1];
            if (_loc_2)
            {
                delete this._bmdMap[param1];
                _loc_2.dispose();
            }
            return;
        }// end function

        public function cacheClips(param1:String, param2:Vector.<BitmapData>) : void
        {
            if (param2)
            {
                this._clipsMap[param1] = param2;
            }
            return;
        }// end function

        public function destroyClips(param1:String) : void
        {
            var _loc_3:BitmapData = null;
            var _loc_2:* = this._clipsMap[param1];
            if (_loc_2)
            {
                for each (_loc_3 in _loc_2)
                {
                    
                    _loc_3.dispose();
                }
                _loc_2.length = 0;
                delete this._clipsMap[param1];
            }
            return;
        }// end function

        public function destroyXMLClips(param1:String) : void
        {
            var _loc_2:* = this._xmlClipMap[param1];
            if (_loc_2)
            {
                _loc_2.dispose();
                delete this._xmlClipMap[param1];
            }
            return;
        }// end function

    }
}
